<template>
  <div>
    <div class="section1">
      <div class="sub-title-1">
        <div class="content">
          <div class="sub-title-1-image"></div>
          <span class="sub-title-1-text">全媒体矩阵传播力</span>
        </div>
        <div class="tab">
          <div
            class="tab-item"
            v-for="(item, index) in tabItems"
            :key="index"
            :class="{ active: activeIndex === index }"
          >
            <div class="image">
              <div class="title" @click="setActive(index)">
                {{ item.title }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="section2">
      <div
        class="section2-part1"
        v-for="(item, index) in totalData"
        :key="index"
      >
        <div
          class="image"
          :style="{ backgroundImage: 'url(' + imageUrls[index] + ')' }"
        ></div>
        <div class="content">
          <div class="top">
            <div class="name">{{ item.name }}</div>
            <div class="trend">
              <div 
                class="number" 
                :class="{ 
                  'trend-up': item.dodTrend === 1, 
                  'trend-down': item.dodTrend === -1,
                  'trend-flat': item.dodTrend === 0
                }"
              >
                {{ item.trend }}
              </div>
              <div
                class="icon"
                v-if="item.dodTrend !== 0"
                :style="{
                  backgroundImage:
                    'url(' +
                    (item.dodTrend === 1 ? trendIcons.up : trendIcons.down) +
                    ')',
                }"
              ></div>
            </div>
          </div>
          <div class="bottom">
            <div class="number">{{ formatNumber(item.number) }}</div>
            <div class="unit" v-if="shouldShowUnit(item.number)">万</div>
          </div>
        </div>
      </div>
    </div>
    <div class="section3">
      <CenterTurntable />
    </div>
  </div>
</template>

<script>
import CenterTurntable from "../components/center-center-turntable.vue";
import { mapMutations } from "vuex";
import { currentPOST } from "api/modules";

export default {
  components: {
    CenterTurntable,
  },
  data() {
    return {
      activeIndex: 2,
      tabItems: [
        { title: "今日", value: "0" },
        { title: "近7日", value: "7" },
        { title: "近30日", value: "30" },
      ],
      totalData: [
        { name: "阅读量", trend: "0.05%", number: "25043", dodTrend: 1 },
        { name: "点赞量", trend: "2.40%", number: "980", dodTrend: 0 },
        { name: "转发量", trend: "-28.71%", number: "9485", dodTrend: -1 },
      ],
    };
  },
  computed: {
    imageUrls() {
      return [
        require("../../assets/img/center/top-ydl-icon.png"),
        require("../../assets/img/center/top-dzl-icon.png"),
        require("../../assets/img/center/top-zfl-icon.png"),
      ];
    },
    trendIcons() {
      return {
        up: require("../../assets/img/trend-up.png"),
        down: require("../../assets/img/trend-down.png"),
      };
    },
  },
  mounted() {
    this.getTotalData();
    this.setLastDay(this.tabItems[this.activeIndex].value);
  },
  methods: {
    ...mapMutations(["SET_LAST_DAY"]),
    
    getTotalData() {
      currentPOST("centerTopTotalHome", {
        lastDay: this.tabItems[this.activeIndex].value,
      })
        .then((res) => {
          if (res.code === 0) {
            this.processTotalData(res.data);
          } else {
            this.$Message({
              text: res.msg,
              type: "warning",
            });
          }
        })
        .catch((error) => {
          console.error("中心顶部数据请求失败:", error);
        });
    },

    processTotalData(apiData) {
      if (!apiData || !Array.isArray(apiData)) {
        console.warn("中心顶部数据格式不正确");
        return;
      }

      this.totalData = apiData.map((item, index) => {
        const trendSign = item.dodTrend === 1 ? '+' : '';
        const trendValue = `${trendSign}${item.dodValue}`;
        
        return {
          name: item.title,
          number: item.value,
          trend: trendValue,
          dodTrend: item.dodTrend,
          dodValue: item.dodValue
        };
      });
    },

    // 格式化数字显示
    formatNumber(num) {
      const number = parseFloat(num);
      if (isNaN(number)) return num;
      
      // 如果数字大于等于10000，转换为万单位并保留1位小数
      if (number >= 10000) {
        return (number / 10000).toFixed(1);
      }
      // 小于10000，直接显示原数字
      return number.toString();
    },

    // 判断是否显示"万"单位
    shouldShowUnit(num) {
      const number = parseFloat(num);
      if (isNaN(number)) return false;
      
      // 只有当数字大于等于10000时才显示"万"单位
      return number >= 10000;
    },

    setActive(index) {
      this.activeIndex = index;
      const lastDay = this.tabItems[index].value;
      this.SET_LAST_DAY(lastDay);
      this.getTotalData();
    },

    setLastDay(lastDay) {
      this.SET_LAST_DAY(lastDay);
    },
  },
};
</script>

<style lang="scss" scoped>
.section1 {
  margin-left: -20px;
  margin-top: 30px;
  margin-right: -200px;
  width: 900px;

  .sub-title-1 {
    display: flex;
    justify-content: space-between;

    .content {
      display: flex;
      align-items: center;

      .sub-title-1-image {
        width: 30px;
        height: 30px;
        background-image: url("../../assets/img/left/section-icon.png");
      }

      .sub-title-1-text {
        width: 128px;
        height: 16px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        font-size: 16px;
        color: #ffffff;
      }
    }

    .tab {
      display: flex;
      text-align: center;
      align-items: center;
      justify-content: space-evenly;
      padding: 20px 130px;

      .tab-item {
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;

        .image {
          height: 24px;
          width: 82px;
          background-size: cover;
          background-image: url("../../assets/img/center/top-tab-un.png");
        }

        .title {
          margin-top: -10px;
          font-weight: 400;
          font-size: 12px;
          color: #a6bed2;
        }
      }

      & .tab-item.active {
        .image {
          background-image: url("../../assets/img/center/top-tab-in.png");
        }

        .title {
          font-weight: 400;
          font-size: 13px;
          color: #ffffff;
        }
      }
    }
  }
}

.section2 {
  margin-left: -20px;
  width: 750px;
  display: flex;
  justify-content: space-between;
  margin-top: 15px;

  .section2-part1 {
    display: flex;
    align-items: center;

    .image {
      height: 111px;
      width: 111px;
    }

    .content {
      margin-top: 20px;
      line-height: 30px;

      .top {
        display: flex;

        .name {
          font-size: 20px;
          color: #ffffff;
          background: linear-gradient(
            to right,
            #4d699d 1%,
            #7fc0f3 10%,
            #d7edff 100%
          );
          -webkit-background-clip: text;
          -webkit-text-fill-color: transparent;
        }

        .trend {
          display: flex;
          margin: auto;
          margin-left: 10px;
          align-items: center;

          .number {
            font-weight: 400;
            font-size: 14px;
            
            &.trend-up {
              color: #f5222d; // 上升红色
            }
            
            &.trend-down {
              color: #52c41a; // 下降绿色
            }
            
            &.trend-flat {
              color: #faad14; // 平缓黄色
            }
          }

          .icon {
            width: 11px;
            height: 15px;
            background-size: cover;
          }
        }
      }

      .bottom {
        display: flex;
        margin: auto;
        align-items: baseline;

        .number {
          font-weight: bold;
          font-size: 30px;
          color: #5096f7;
        }

        .unit {
          margin-left: 5px;
          font-weight: 400;
          font-size: 16px;
          color: #ffffff;
        }
      }
    }
  }
}

.section3 {
}
</style>